package nl.rainbowsheep.reisorg.cust.site.service;

import java.util.ArrayList;
import java.util.List;
import nl.rainbowsheep.reisorg.cust.site.authentication.BasicAuthInterceptor;
import nl.rainbowsheep.reisorg.cust.site.model.BurritoUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class UserService {

    @Value("${api.url}")
    String url;
    
    /**
     * use slf4j logger during debugging
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);


    public BurritoUser saveUser(BurritoUser user) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        return template.postForObject(url + "user/save", user, BurritoUser.class);
    }
    
    public BurritoUser getUserById(long id) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        BurritoUser burritoUser = template.getForObject(url + "user/getUserById?id=" + id, BurritoUser.class);
        LOGGER.debug("CUSTSITE SERVICE + " + burritoUser.getUsername());
        return burritoUser;
    }
    
    public BurritoUser insertBurritoUser(BurritoUser user) {
        RestTemplate template = new RestTemplate();
        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add(new BasicAuthInterceptor());
        template.setInterceptors(interceptors);

        BurritoUser burritoUser = template.postForObject(url + "user/save", user, BurritoUser.class);
        return burritoUser;
        
    }
    
}
